<?php
/**
 * 文件管理-紫鸟文件（已停用）
 * (model)
 */
namespace App\Models\File;

use App\Func\UploadFile;
use App\Models\Basics\SysCountry;
use Illuminate\Database\Eloquent\Model;
class FileZiniao extends Model
{
    protected $table = 'file_ziniao';

    protected $guarded = [];

    public function country()
    {
        return $this->belongsTo(SysCountry::class,'country_id');
    }

    public static $_rpaId = [
        '1361' => '业绩通知详情明细',
    ];

    public static $_status = [
        1 => '未解析',
        2 => '已解析',
    ];

    public static function fileDir($time){
        return 'ziniao'.DIRECTORY_SEPARATOR.date('Ymd',$time);
    }

    public static function fileName($countryId,$rpaId,$fileName){
        return $countryId.'-'.$rpaId.'-'.$fileName;
    }

    protected static function boot()
    {
        parent::boot();

        static::deleting(function ($model){
            if($model->status == 2){
                throw new \Exception('文件已解析无法删除');
            }
            $uf = UploadFile::getInit();
            $fileDir = self::fileDir(strtotime($model->file_date));
            $fileName = self::fileName($model->country_id,$model->rpa_id,$model->file_name);
            $flag = $uf->delFile($fileDir.DIRECTORY_SEPARATOR.$fileName);
            if(!$flag){
                throw new \Exception('删除失败');
            }
        });
    }
}
